package com.yvdedu.hospital.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.yvdedu.hospital.entity.Login;
import com.yvdedu.hospital.entity.Role;
import com.yvdedu.hospital.entity.User;
import com.yvdedu.hospital.service.LoginService;


@Controller
@SessionAttributes("username")
public class LoginController {
	
	@Resource
	LoginService loginService;
	List<Login> list;
	
//	管理员权限index
	@RequestMapping("admin")
	public void index ( Model model ){
		List<User> user = loginService.userquery(list.get(0).getId());
		model.addAttribute("username",user.get(0).getUserName());
		List<Role> role = loginService.rolequery(user.get(0).getRole_id());
		model.addAttribute("rolename",role.get(0).getRoleName());
	}
	
//	医生权限index
	@RequestMapping("index")
	public void indexs ( Model model ){
		List<User> user = loginService.userquery(list.get(0).getId());
		model.addAttribute("username",user.get(0).getUserName());
		List<Role> role = loginService.rolequery(user.get(0).getRole_id());
		model.addAttribute("rolename",role.get(0).getRoleName());
	}
	
//	登录页面
	@RequestMapping("login")
	public void login (){
	}

//	登录操作
	@RequestMapping("dologin")
	public Object dologin ( Login login,Model model ){
//		HttpServletRequest request
//		System.out.println(login);
		
		list = loginService.login(login);
//		System.out.println(list);
//		
//		if ( list.size()<1 ){
//			model.addAttribute("data","账号密码错误,请重新登录！");
//			return "login";
//		}else {
//			request.getSession().setAttribute("access", list.get(0).getId());
//			
//			List<User> user = loginService.userquery(list.get(0).getId());
//			if(user.get(0).getRole_id()==1){
//				return "redirect:index/sadmin";
//			}
//			return "redirect:index/admin";
//		}
		
		
//		shiro安全框架调用
		Subject subject = SecurityUtils.getSubject();
		
//		判断是否已经登录
		if(  subject.isAuthenticated() ){
			return "redirect:index";
		}
		
//		验证登录
		UsernamePasswordToken token = new UsernamePasswordToken(login.getLogin(),login.getPassword());
		try {
			subject.login(token);
			
		
		} catch (IncorrectCredentialsException ice) {
			model.addAttribute("login",login.getLogin());
			model.addAttribute("data","用户名或密码不正确");
			return "login";
		}
		return "redirect:index";
		
	}
	
//	退出登录
	@RequestMapping("outlogin")
	public Object outlogin (){
		Subject subject = SecurityUtils.getSubject();
		subject.logout();
		return "redirect:login";
	}
}
